#include <stdio.h>
#include <curses.h>
#include <string.h>
#include <stdlib.h>
#include "wrizhi.h"
#include "lianjie.h"
ssize_t my_read(int fd, char *buf, size_t count) {
    ssize_t newlen;
    ssize_t len = read(fd, buf, count);
    while (len < count) {
        if (len > 0) {
            newlen = read(fd, buf + len, count - len);
            len += newlen;
        } else {
            return -1;
        }
    }
    return len;
}
int main(int argv, char **argc) {
    //上65下66右67左68
    getrizhi();
    int fd;
    char mm[8] = {0};
    if((fd = getsock(argc[1], argc[2]))==-1){
        wrizhi("连接失败！\n");
        return -1;
    }
    printf("请输入密码：");
    scanf("%[^\n]%*c", mm);
    write(fd, mm, sizeof(mm));
    memset(mm, 0, sizeof(mm));
    my_read(fd, (char *)mm, sizeof(mm));
    wrizhi("mm:%s\n",mm);
    if (strcmp(mm, "yyyyyy") != 0) {
        printf("密码错误！\n");
        return -1;
    }else{
        printf("连接成功！\n");
    }
    sleep(2);
    initscr();
    int a;
    while(1){
        a=getch();
        clear();
        move(0,0);
        printw("%d\n",a);
        wrizhi("%d\n",a);
        usleep(50000);
        write(fd,&a,sizeof(a));
        if(a==116){
            break;
        }
    }
    close(fd);
    rmrizhi();
    endwin();
    return 0;
}
